package com.netty.pro.netty.buf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;

import java.nio.charset.Charset;

/**
 * @author xuzn
 * @ClassName: NettyByteBuf_02
 * @ProjectName netty-pro
 * @date 2020/3/15/9:31
 */
public class NettyByteBuf_02 {
  public static void main(String[] args) {
    ByteBuf buffer = Unpooled.copiedBuffer("hello,netty", CharsetUtil.UTF_8);
    if (buffer.hasArray()) {
      // 返回buffer中的内容
      byte[] content = buffer.array();
      System.out.println(new String(content, CharsetUtil.UTF_8));
      // buffer的真实实例
      System.out.println("buffer:" + buffer);
      System.out.println("arrayOffset:" + buffer.arrayOffset());
      System.out.println("readerIndex:" + buffer.readerIndex());
      System.out.println("writerIndex:" + buffer.writerIndex());
      System.out.println("capacity:" + buffer.capacity());
      System.out.println(buffer.getByte(0));
      System.out.println((char) buffer.getByte(0));
      // 可读字节数
      int readableBytes = buffer.readableBytes();
      System.out.println("readableBytes:" + readableBytes);
      buffer.readByte();
      System.out.println("readByte操作之后readableBytes:" + readableBytes);
      // 第一个参数代表从哪里开始,第二个参数代表读几个字节
      System.out.println(buffer.getCharSequence(4, 4, CharsetUtil.UTF_8));
    }
  }
}
